Imports RIBS.DataAccessLayer

Namespace BusinessLogicLayer.Reports
    Public Class StatusReport
#Region "*********************************************************** Methods "
        Public Shared Function StatusReport(ByVal ds As DataSet, Optional ByVal UserBased As Boolean = True) As DataSet
            Try
                ' validate input
                'If RibsID = String.Empty Then
                '    Throw New ArgumentOutOfRangeException("username")
                'End If
                Dim DBLayer As New DataAccess
                Dim tblOptions As DataTable = ds.Tables("Options")
                Dim tblUser As DataTable = ds.Tables("Users")
                Dim tblAction As DataTable = ds.Tables("Action")
                Dim rtds As New dsDBSchema
                Dim RibsID As Int32 = 0
                Dim LetterCode As String = ""
                'Dim Policy_Number As String = ""
                Dim Actions As String = ""

                For Each dr As DataRow In tblAction.Rows
                    Actions += dr("Action") & ","
                Next

                For Each rowUser As DataRow In tblUser.Rows

                    Dim drOptions As DataRow
                    drOptions = rtds.Tables("Options").NewRow
                    drOptions("UserID") = rowUser("User")
                    drOptions("FromDate") = tblOptions.Rows(0).Item("FromDate")
                    drOptions("ToDate") = tblOptions.Rows(0).Item("ToDate")
                    drOptions("ReportTitle") = tblOptions.Rows(0).Item("ReportTitle")
                    drOptions("PageTitle") = tblOptions.Rows(0).Item("PageTitle")

                    rtds.Tables("Options").Rows.Add(drOptions)


                    Dim dt As DataTable = Nothing
                    dt = DBLayer.GetUserWorkByDateAndContactMethod(rowUser("User"), _
                                                                    tblAction, _
                                                                    tblOptions.Rows(0).Item("FromDate"), _
                                                                    tblOptions.Rows(0).Item("ToDate"), _
                                                                    UserBased)
                    For Each rowUserWork As DataRow In dt.Rows

                        'Dim iCol As Int16 = dt.Columns("Total Calls Made").Ordinal + 1
                        If RibsID = rowUserWork("RibsID") Then
                            For Each CurrentRow As DataRow In rtds.Tables("UserEffort").Rows
                                If CurrentRow("RibsID") = RibsID Then
                                    If Globals.ReportContactMethod(rowUserWork("ContactMethod")) = Globals.ReportDisplay.Display Then
                                        CurrentRow(rowUserWork("ContactMethod")) += 1
                                        CurrentRow("Total Calls Made") += 1
                                        Exit For
                                    ElseIf Globals.ReportContactMethod(rowUserWork("ContactMethod")) = Globals.ReportDisplay.AddToOther Then
                                        CurrentRow("Other") += 1
                                        CurrentRow("Total Calls Made") += 1
                                        Exit For
                                    ElseIf Globals.ReportContactMethod(rowUserWork("ContactMethod")) = Globals.ReportDisplay.DontDisplay Then
                                        ' Do nothing
                                    End If
                                    
                                End If
                            Next
                        Else
                            'User Work
                            Dim drUserWork As DataRow
                            drUserWork = rtds.Tables("UserEffort").NewRow
                            drUserWork("UserID") = rowUser("User").ToString.ToUpper
                            drUserWork("Letter_Code") = rowUserWork("Letter_Code").ToString.ToUpper
                            drUserWork("Action") = rowUserWork("TopAction")
                            drUserWork("ManuallyAdded") = IIf(rowUserWork("ManuallyAdded"), "*", "")
                            drUserWork("Account_SYSTEM") = rowUserWork("Account_SYSTEM").ToString.ToUpper
                            drUserWork("NEW_PREMIUM_AMOUNT") = rowUserWork("NEW_PREMIUM_AMOUNT")
                            drUserWork("AMOUNT_Due") = rowUserWork("AMOUNT_Due")
                            drUserWork("WriteOff") = rowUserWork("WriteOff")
                            drUserWork("Retained") = rowUserWork("Retained")
                            drUserWork("Policy_Number") = rowUserWork("Policy_Number")
                            drUserWork("RibsID") = rowUserWork("RibsID")
                            For iCtr As Int16 = rtds.Tables("UserEffort").Columns("Total Calls Made").Ordinal To rtds.Tables("UserEffort").Columns.Count - 1
                                drUserWork(rtds.Tables("UserEffort").Columns(iCtr).ColumnName) = 0
                            Next
                            If Globals.ReportContactMethod(rowUserWork("ContactMethod")) = Globals.ReportDisplay.Display Then
                                drUserWork(rowUserWork("ContactMethod")) = 1
                                drUserWork("Total Calls Made") = 1
                            ElseIf Globals.ReportContactMethod(rowUserWork("ContactMethod")) = Globals.ReportDisplay.AddToOther Then
                                drUserWork("Other") += 1
                                drUserWork("Total Calls Made") += 1
                            ElseIf Globals.ReportContactMethod(rowUserWork("ContactMethod")) = Globals.ReportDisplay.DontDisplay Then
                                ' DoNothing 
                            End If
                            rtds.Tables("UserEffort").Rows.Add(drUserWork)

                            Summary(rtds, rowUserWork, tblOptions)

                            RibsID = rowUserWork("Ribsid").ToString
                            LetterCode = rowUserWork("Letter_Code").ToString
                        End If
                    Next ' Work
                Next 'User 

                Return rtds
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Private Shared Function Summary(ByRef rtds As dsDBSchema, _
                                        ByVal rowUserWork As DataRow, _
                                        ByVal tblOptions As DataTable) As Boolean
            'ByVal rowUser As DataRow, _
            Try
                Dim bSystemUpdated As Boolean = False
                For Each drSummary As DataRow In rtds.Tables("SummarySystem").Rows
                    If (drSummary("Account_SYSTEM").ToString.ToUpper.Trim = rowUserWork("Account_SYSTEM").ToString.ToUpper.Trim) And _
                        (drSummary("UserID").ToString.ToUpper = rowUserWork("UserID").ToString.ToUpper) Then
                        drSummary("Account_SYSTEMCount") += 1
                        drSummary("Account_SYSTEMNEW_PREMIUM_AMOUNT") += rowUserWork("NEW_PREMIUM_AMOUNT")
                        drSummary("Account_SYSTEMRetained") += rowUserWork("Retained")
                        drSummary("Account_SYSTEMWriteOff") += rowUserWork("WriteOff")
                        bSystemUpdated = True
                        Exit For
                    End If
                Next ' Summary
                If Not bSystemUpdated Then
                    Dim drUserSummary As DataRow
                    drUserSummary = rtds.Tables("SummarySystem").NewRow
                    drUserSummary("UserID") = rowUserWork("UserID").ToString.ToUpper
                    drUserSummary("Account_SYSTEM") = rowUserWork("Account_SYSTEM").ToString.Trim
                    drUserSummary("Account_SYSTEMCount") = 1
                    drUserSummary("Account_SYSTEMNEW_PREMIUM_AMOUNT") = rowUserWork("NEW_PREMIUM_AMOUNT")
                    drUserSummary("Account_SYSTEMRetained") = rowUserWork("Retained")
                    drUserSummary("Account_SYSTEMWriteOff") = rowUserWork("WriteOff")
                    rtds.Tables("SummarySystem").Rows.Add(drUserSummary)
                End If

                Dim bActionUpdated As Boolean = False
                For Each drSummary As DataRow In rtds.Tables("SummaryAction").Rows
                    If (drSummary("Action") = rowUserWork("TopAction")) And _
                        (drSummary("UserID").ToString.ToUpper = rowUserWork("UserID").ToString.ToUpper) Then
                        drSummary("ActionCount") += 1
                        drSummary("ActionNEW_PREMIUM_AMOUNT") += rowUserWork("NEW_PREMIUM_AMOUNT")
                        drSummary("ActionRetained") += rowUserWork("Retained")
                        drSummary("ActionWriteOff") += rowUserWork("WriteOff")
                        bActionUpdated = True
                        Exit For
                    End If
                Next ' Summary
                If Not bActionUpdated Then
                    Dim drUserSummary As DataRow
                    drUserSummary = rtds.Tables("SummaryAction").NewRow
                    drUserSummary("UserID") = rowUserWork("UserID").ToString.ToUpper
                    drUserSummary("ToDate") = tblOptions.Rows(0).Item("FromDate")
                    drUserSummary("FromDate") = tblOptions.Rows(0).Item("ToDate")
                    drUserSummary("Action") = rowUserWork("TopAction")
                    drUserSummary("ActionCount") = 1
                    drUserSummary("ActionNEW_PREMIUM_AMOUNT") = rowUserWork("NEW_PREMIUM_AMOUNT")
                    drUserSummary("ActionRetained") = rowUserWork("Retained")
                    drUserSummary("ActionWriteOff") = rowUserWork("WriteOff")
                    rtds.Tables("SummaryAction").Rows.Add(drUserSummary)

                End If
                'Full Summary
                Dim Retained As Double = 0
                bActionUpdated = False
                For Each drSummary As DataRow In rtds.Tables("Summary").Rows
                    If (drSummary("Action") = rowUserWork("TopAction")) And _
                        (drSummary("UserID").ToString.ToUpper = rowUserWork("UserID").ToString.ToUpper) Then
                        If rowUserWork("TopAction").ToString.ToUpper = "RETAINED" Then
                            Retained = rowUserWork("Retained")
                        Else
                            Retained = rowUserWork("NEW_PREMIUM_AMOUNT")
                        End If
                        drSummary("ActionCount") += 1
                        drSummary("ActionRetained") += rowUserWork("Retained")
                        drSummary("ActionWriteOff") += rowUserWork("WriteOff")

                        drSummary("ActionNEW_PREMIUM_AMOUNT") += Retained
                        If rowUserWork("Account_SYSTEM").ToString.Trim.ToUpper = "ECLIPSE" Then
                            drSummary("Eclipse") += Retained
                        ElseIf rowUserWork("Account_SYSTEM").ToString.Trim.ToUpper = "MAINFRAME" Then
                            drSummary("MAINFRAME") += Retained
                        Else
                            drSummary("Other") += Retained
                        End If

                        If rowUserWork("Account_SYSTEM").ToString.Trim.ToUpper = "ECLIPSE" Then
                            drSummary("EclipseCount") += 1
                        ElseIf rowUserWork("Account_SYSTEM").ToString.Trim.ToUpper = "MAINFRAME" Then
                            drSummary("MAINFRAMECount") += 1
                        Else
                            drSummary("OtherCount") += 1
                        End If
                        bActionUpdated = True
                        Exit For
                    End If
                Next ' Summary
                If Not bActionUpdated Then
                    Dim drUserSummary As DataRow
                    If rowUserWork("TopAction").ToString.ToUpper = "RETAINED" Then
                        Retained = rowUserWork("Retained")
                    Else
                        Retained = rowUserWork("NEW_PREMIUM_AMOUNT")
                    End If
                    drUserSummary = rtds.Tables("Summary").NewRow
                    drUserSummary("UserID") = rowUserWork("UserID").ToString.ToUpper
                    drUserSummary("ToDate") = tblOptions.Rows(0).Item("FromDate")
                    drUserSummary("FromDate") = tblOptions.Rows(0).Item("ToDate")
                    drUserSummary("Action") = rowUserWork("TopAction")
                    drUserSummary("ActionCount") = 1
                    drUserSummary("ActionNEW_PREMIUM_AMOUNT") = Retained
                    drUserSummary("ActionRetained") = rowUserWork("Retained")
                    drUserSummary("ActionWriteOff") = rowUserWork("WriteOff")
                    
                    If rowUserWork("Account_SYSTEM").ToString.Trim.ToUpper = "ECLIPSE" Then
                        drUserSummary("Eclipse") = Retained
                    ElseIf rowUserWork("Account_SYSTEM").ToString.Trim.ToUpper = "MAINFRAME" Then
                        drUserSummary("MAINFRAME") = Retained
                    Else
                        drUserSummary("Other") = Retained
                    End If
                    If rowUserWork("Account_SYSTEM").ToString.Trim.ToUpper = "ECLIPSE" Then
                        drUserSummary("EclipseCount") = 1
                    ElseIf rowUserWork("Account_SYSTEM").ToString.Trim.ToUpper = "MAINFRAME" Then
                        drUserSummary("MAINFRAMECount") = 1
                    Else
                        drUserSummary("OtherCount") = 1
                    End If
                    rtds.Tables("Summary").Rows.Add(drUserSummary)
                End If

            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Private Shared Function SystemSummary(ByVal rtds As dsDBSchema, ByVal dr As DataRow) As dsDBSchema
            Try
                Dim ds As dsDBSchema = rtds.Clone

                Dim bSystemUpdated As Boolean = False
                For Each drSummary As DataRow In rtds.Tables("SummarySystem").Rows
                    If (drSummary("Account_SYSTEM").ToString.ToUpper = dr("Account_SYSTEM").ToString.ToUpper) And (drSummary("UserID").ToString.ToUpper = dr("User").ToString.ToUpper) Then
                        drSummary("Account_SYSTEMCount") += 1
                        drSummary("Account_SYSTEMNEW_PREMIUM_AMOUNT") += dr("NEW_PREMIUM_AMOUNT")
                        drSummary("Account_SYSTEMRetained") += dr("Retained")
                        drSummary("ActionWriteOff") += dr("WriteOff")
                        bSystemUpdated = True
                        Exit For
                    End If
                Next ' Summary
                If Not bSystemUpdated Then
                    Dim drUserSummary As DataRow
                    drUserSummary = rtds.Tables("SummarySystem").NewRow
                    drUserSummary("UserID") = dr("User").ToString.ToUpper
                    drUserSummary("Account_SYSTEM") = dr("Account_SYSTEM").ToString
                    drUserSummary("Account_SYSTEMCount") = 1
                    drUserSummary("Account_SYSTEMNEW_PREMIUM_AMOUNT") = dr("NEW_PREMIUM_AMOUNT")
                    drUserSummary("Account_SYSTEMRetained") = dr("Retained")
                    rtds.Tables("SummarySystem").Rows.Add(drUserSummary)
                End If

                Return ds
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Shared Function GetLetterSentByID(ByVal RibsID As Int32) As LetterSentCollection
            ' validate input
            'If RibsID = String.Empty Then
            '    Throw New ArgumentOutOfRangeException("username")
            'End If
            Dim DBLayer As New DataAccess ' DataAccessLayerBaseClass = DataAccessLayerBaseClassHelper.GetDataAccessLayer()
            Return DBLayer.GetRibsItems(RibsID)
        End Function

#End Region

    End Class
End Namespace

